import pymysql
try:
    conn = pymysql.connect(host="127.0.0.1",
                           port=3306,
                           user="root",
                           password="root",
                           database="books",
                           charset="utf8",)
                           # autocommit=True)
    if conn:
        cursor = conn.cursor()
    conn.begin()
    sql = "insert into t_book(id,title,pub_date) values(4,'西游记','1986-01-01')"
    cursor.execute(sql)
    print(cursor.rowcount)
    sql = "insert into t_book(id,title,pub_date) values(4,'红楼梦','1987-02-02')"
    cursor.execute(sql)
    print(cursor.rowcount)
    conn.commit()
except Exception as f:
    print(f)
    conn.rollback()
finally:
    cursor.close()
    conn.close()
